<?php
class Homepage
{
    /**
     * @var object Zend DB Adapter
     */
    private $_db;
    /**
     * @var string
     */
   
    public function __construct()
    {
		$this->_db = Zend_Registry::get('db');
    }
    public function addhomepagecontent($insertarr)
    {
	     try
	     {
	     	$sql= "INSERT INTO aml_homepage(
	               title ,
	               content ,
	               section ,
	               sub_section,
	               isactive
	               ) 
	               VALUES (	                
	                '{$insertarr['title']}',
	                '{$insertarr['content']}',
	                {$insertarr['section']},
	                {$insertarr['sub_section']},
	                {$insertarr['isactive']}
	               ) ";
	          if($this->_db->exec($sql))
	          {
	              return true;
	          }
	          else
	          {
	              return false;
	          }
	     }
	     catch(Exception $e)
	     {
	          echo 'Caught exception: ',  $e->getMessage(), "\n";
	         return false;
	     }
    }

	public function edithomepage($insertarr)
	{	
		try
		{
			$sql= " 
			UPDATE aml_homepage
			SET title='{$insertarr['title']}' ,
				content='{$insertarr['content']}',
				section={$insertarr['section']},
				sub_section={$insertarr['sub_section']},
				isactive={$insertarr['isactive']}
			WHERE id={$insertarr['id']}";
			if($this->_db->exec($sql))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		catch(Exception $e)
		{
			echo 'Caught exception: ',  $e->getMessage(), "\n";
			return false;
		}
	}
	
    public function listhomepagecontent()
    {
        $sql=" select * from aml_homepage ";
        return $this->_db->fetchAll($sql);
    }
    
    public function gethomepagebyid($id)
	{
		$sql=" select * from aml_homepage where id={$id}"; 
		return $this->_db->fetchRow($sql);	
	}
	
    public  function deletehomepage($id)
    {
		$sql=" delete from aml_homepage where id={$id}";
		
		if($this->_db->exec($sql))
		{
			return true;
		}
		else
		{
			return false;
		}
    }
    
    public function changehomepagestatus($id)
    {
		$sql_getstatus = "SELECT isactive
						  FROM aml_homepage
						  WHERE  id = " . $id;
		
		$result = $this->_db->fetchRow($sql_getstatus);
		$status = $result['isactive'];
		
		if($status == 1) {
			$change_status = 0;
		} else {
			$change_status = 1;
		}
		
		$sql_updatestatus = "UPDATE aml_homepage
			 SET isactive = " . $change_status . "
			 WHERE  id = " . $id;
		$this->_db->exec($sql_updatestatus);	
    }

	public function gethomepagesections() 
	{	
		$sql=" SELECT id,title FROM aml_homesections where parentid = 0 AND status=1 ";	
		$sectionlist=$this->_db->fetchAll($sql) ;	
		
		return $sectionlist;	
	}
	
	// homepage content edit form - get sub section array
	
	public function getsubsectionshomepage($id)
    {
    	$sql = "SELECT aml_homesections.id,aml_homesections.title FROM aml_homesections 
				LEFT JOIN aml_homepage ON aml_homesections.parentid  = aml_homepage.section
				WHERE aml_homepage.id = " . $id;
    	$subsectionlist = $this->_db->fetchAll($sql) ;	
		
		return $subsectionlist;
    }
	
	public function gethomepagesubsections($sectid)
	{
		$sql=" SELECT id,title 
			   FROM aml_homesections 
			   WHERE parentid = " . $sectid . " 
			   AND status=1";	
		
		$subsectlist = $this->_db->fetchAll($sql) ;	
		
		return $subsectlist;
	}
    
    public function addTabData($form_values)
    {
    	$title = addslashes($form_values['title']);
		$content = addslashes($form_values['content']);
		$section = $form_values['section'];
		$status = $form_values['isactive'];
		if($status == "")
		{
			$status = 0;
		}
		
		$sql = "INSERT INTO aml_homepage(title, content, section, isactive)
				VALUES('" . $title . "', '" . $content . "', " . $section . ", '" . $status . "')";
		$add_news = $this->_db->exec($sql);	
    }
	
}